<?php
//测试控制器类
class LoginController extends Controller{
    private $db;
    //构造函数
    public function __init(){
        //判断是否登陆
        $user = Q('session.user');
        if(!empty($user)){
            header('location:'.u('index/index'));
        }
        //指定函数
        $this -> db = k("User");
    }
/*-----------------登陆页面-----------------*/
    //显示登陆页面
    public function login(){
        //显示视图
        $this->display('header');
        $this->display();
    }

    //登陆验证
    public function into(){

        $Model = K('user');

        //表单验证
        if(!$Model -> create()){
            $this -> error($Model -> error);
            exit;
        }

        //获取用户信息
        $userinfo = $this -> db -> userone('uname ="'.$_POST['username'].'" and type = "home"');

        //判断用户是否存在
        if(!$userinfo){
            $this -> error('账号不存在，请先注册后再登录！');
            exit;
        }

        //判断密码是否正确
        if(md5(md5($_POST['userpass'])) != $userinfo['pw']){
            $this -> error('账号或者密码错误！');
            exit;
        }else{
            /*登陆信息（ip，登陆时间等）*/
            //获取ip
            $logip = $_SERVER["REMOTE_ADDR"];
            //获取登录时间
            $logtime = time();
            //判断数据库是否有登陆信息
            $where = 'uid = '.$userinfo['uid'];
            $loginlog = $this -> db -> selclog($where);
            //修改登陆信息
            $data = array(
                'uid' => $userinfo['uid'],
                'logip' => $logip,
                'logtime' => $logtime,
            );
            if(!empty($loginlog)){
                $where = 'lid = '.$loginlog['lid'].' and uid = '.$userinfo['uid'];
                $this-> db -> uplog($where,$data);
            }else{
                //添加登陆信息
                $this-> db -> addlog($data);
            }

            $_SESSION['user'] = $userinfo;
            $_SESSION['logip'] = $logip;
            //判断跳转位置
            $gid = Q('session.gid');
            unset($_SESSION['gid']);
            if(empty($gid)){
                header('location:'.u('index/index'));
            }else{
                header('location:'.u('Orderform/addorder',array('gid'=>$gid)));
            }
        }

    }
/*-----------------注册页面----------------*/
    //注册用户页面
    public function reg(){
        //显示视图
        $this->display('header');
        $this->display();
    }
    //添加用户
    public function add(){
        $Model = K('user');

        //表单验证
        if(!$Model -> create()){
            $this -> error($Model -> error);
            exit;
        }

        //判断邀请码是否正确
        $code = $_POST['invt'];
        $where = 'code = "'.$code.'"';
        $invt = $this -> db -> codeone($where);
        if(empty($invt)){
            $this -> error('邀请码不正确！');
            exit;
        }

        //判断是否被人注册
        $userinfo = $this->db->userone('uname="'.$_POST['username'].'"');
        if(!empty($userinfo)){
            $this -> error('账号已被注册！');
            exit;
        }

        $regtime = time();
        //将获取到的数据放入数组
        $data = array(
            'uname' => $_POST['username'],
            'pw' => md5(md5($_POST['userpass'])),
            'uvip' => $invt['type'],
            'addtime' => $regtime,
            'type' => 'home'
        );
        //添加用户
        if($this -> db -> adduser($data)){
            $this -> db -> codedel($where);
            $this -> success($message = '注册成功！将跳转到登陆页面进行登陆。',U('login'), $time = 2);
        }else{
            $this -> error('账户注册失败！');
            exit;
        }
    }

    public function logout(){
        unset($_SESSION['user']);
        $this -> success($message = '退出成功',U('login/login'), $time = 2);
    }
}
